package edu.zju.gis.dbfg.queryserver.controller;

import edu.zju.gis.dbfg.common.Result;
import edu.zju.gis.dbfg.queryserver.tool.RestElasticSearchHelper;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest;
import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/index")
public class IndexController {

    @Autowired
    private RestElasticSearchHelper helper;

    @RequestMapping(value = "clear/scroll/{tag}", method = RequestMethod.POST)
    public Result view(@PathVariable String tag) {
        if (helper.ClearScroll(tag))
            return Result.success();
        else
            return Result.error();
    }

    @RequestMapping(value = "{indexName}/fieldsMap", method = RequestMethod.GET)
    public Result fieldsInfo(@PathVariable String indexName) {
        Map<String, Object> fields = helper.fieldsInfo(indexName);
        if (fields != null)
            return Result.success().setBody(fields);
        else
            return Result.error();
    }
}
